package com.kaixinff.kaixin001.reg.client;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.kaixinff.kaixin001.reg.client.ui.RootLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class KaiXinReg implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final KaiXinRegServiceAsync service = GWT
			.create(KaiXinRegService.class);
	private final KaiXinRegConstants constants = GWT
			.create(KaiXinRegConstants.class);

	private RootLayout rootLayout;
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		SimplePanel panel = new SimplePanel();
		panel.setWidth("100%");
		panel.getElement().setAttribute("align", "center");
		RootPanel.get().add(panel);
		
		rootLayout = new RootLayout();
		panel.add(rootLayout);
		
		Button regMailButton = new Button(constants.regMail(), new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				service.regMail(new AsyncCallback<String>(){

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(String result) {
						rootLayout.addContent(result);
					}});
			}});
		rootLayout.addButton(regMailButton);
	}
}
